#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from app import create_app
from models import db, Problem
from collections import Counter

def show_problem_stats():
    """显示题库统计信息"""
    app = create_app()
    
    with app.app_context():
        problems = Problem.query.all()
        
        print("=" * 60)
        print("📊 题库统计信息")
        print("=" * 60)
        
        # 按类型统计
        type_counter = Counter([p.type for p in problems])
        print(f"\n📝 题型分布 (共 {len(problems)} 道题目):")
        print("-" * 40)
        
        type_names = {
            'choice': '选择题',
            'blank': '填空题', 
            'short': '简答题',
            'code': '编程题'
        }
        
        for problem_type, count in type_counter.most_common():
            type_name = type_names.get(problem_type, problem_type)
            percentage = (count / len(problems)) * 100
            print(f"  {type_name}: {count} 道 ({percentage:.1f}%)")
        
        # 按难度统计
        difficulty_counter = Counter([p.difficulty for p in problems])
        print(f"\n🎯 难度分布:")
        print("-" * 40)
        
        difficulty_names = {
            'easy': '简单',
            'medium': '中等',
            'hard': '困难'
        }
        
        for difficulty, count in difficulty_counter.most_common():
            difficulty_name = difficulty_names.get(difficulty, difficulty)
            percentage = (count / len(problems)) * 100
            print(f"  {difficulty_name}: {count} 道 ({percentage:.1f}%)")
        
        # 按类型和难度组合统计
        print(f"\n📋 题型难度详细分布:")
        print("-" * 50)
        
        for problem_type in ['choice', 'blank', 'short', 'code']:
            type_problems = [p for p in problems if p.type == problem_type]
            if type_problems:
                type_name = type_names.get(problem_type, problem_type)
                print(f"\n  {type_name} ({len(type_problems)} 道):")
                
                type_difficulty_counter = Counter([p.difficulty for p in type_problems])
                for difficulty, count in type_difficulty_counter.most_common():
                    difficulty_name = difficulty_names.get(difficulty, difficulty)
                    print(f"    {difficulty_name}: {count} 道")
        
        # 显示示例题目
        print(f"\n📚 各题型示例题目:")
        print("-" * 50)
        
        for problem_type in ['choice', 'blank', 'short', 'code']:
            type_problems = [p for p in problems if p.type == problem_type]
            if type_problems:
                type_name = type_names.get(problem_type, problem_type)
                print(f"\n  {type_name} 示例:")
                
                # 显示前3个题目作为示例
                for i, problem in enumerate(type_problems[:3], 1):
                    difficulty_name = difficulty_names.get(problem.difficulty, problem.difficulty)
                    print(f"    {i}. {problem.title} ({difficulty_name})")
        
        print(f"\n" + "=" * 60)
        print("✅ 题库已包含所有要求的题型！")
        print("=" * 60)

if __name__ == '__main__':
    show_problem_stats()
